/*
Dibuja un  cubo de altura, ancho, profundidad, color y 
opacidad especificadaa.
El centro del cubo se encuentra en el centro. el mismo
     _______
    /      /| 
   /______/ |
   ||___c___|
   |/     |/
   /______/
   /* VPB = Vertex Position Buffer
	VCB = Vertex Color Buffer 
	VIB = Vertex Index Buffer
	VI  = Vertex Index
 */
  


function Cubo( altura, ancho, profundidad, r,g,b,o){
	this.altura=altura;
	this.ancho=ancho;
	this.profundidad=profundidad;
	
	var x = this.ancho/2.0;
	var y = this.altura/2.0;
	var z = this.profundidad/2.0;
	
	this.VPB = gl.createBuffer();
	gl.bindBuffer(gl.ARRAY_BUFFER, this.VPB);
	//cara sup
	this.vertex=[


			  x, y ,-z,
			 -x, y ,-z,
			 -x, y , z,
			  x, y , z,

	//cara inf		
			  x, -y ,-z,
			 -x, -y ,-z,
			 -x, -y , z,
			  x, -y , z,
			 
	//cara front		
			 x, y , z,
			-x, y , z,
			-x,-y , z,
			 x,-y , z,
			 
	//cara trasera		 
			 x, y ,-z,
			-x, y ,-z,
			-x,-y ,-z,
			 x,-y ,-z,
			 
	//cara izq		 
			 -x, y , z,
			 -x, y ,-z,
			 -x,-y ,-z,
			 -x,-y , z,
	 
	//cara der		 
			   x, y , z,
			   x, y ,-z,
			   x,-y ,-z,
			   x,-y , z
			 ];
			 
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertex), gl.STATIC_DRAW);
			this.VPB.itemSize=3;
			this.VPB.numItem=24;

			
			this.VCB = gl.createBuffer(); 
			gl.bindBuffer(gl.ARRAY_BUFFER, this.VCB); 
			

			var colors = [ 
			[r, g, b, o] 
			];

			//repite cuatro veces por cada cara que es por cada vertice
			var unpackedColors = [];
			for (var i in colors) { 
				var color = colors[i]; 
				for (var j=0; j < this.VPB.numItem; j++) { 
					unpackedColors = unpackedColors.concat(color); 
					} 
			} 

			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW); 

			this.VCB.itemSize = 4;

			this.VCB.numItems = 24;
			
			this.VIB = gl.createBuffer(); 

			gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.VIB); 

			var cubeVI = [ 
			0, 1, 2, 0, 2, 3, // up face 
			4, 5, 6, 4, 6, 7, // down face 
			8, 9, 10, 8, 10, 11, // front face 
			12, 13, 14, 12, 14, 15, // back  face 
			16, 17, 18, 16, 18, 19, // left face 
			20, 21, 22, 20, 22, 23 // right face 
			]; 

			gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cubeVI), gl.STATIC_DRAW); 

			this.VIB.itemSize = 1; 

			this.VIB.numItems = 36; 
			
			this.draw = function(){

				
				gl.bindBuffer(gl.ARRAY_BUFFER,this.VPB ); 
				gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, this.VPB.itemSize, gl.FLOAT, false, 0, 0); 
				gl.bindBuffer(gl.ARRAY_BUFFER, this.VCB); 

				gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, this.VCB.itemSize, gl.FLOAT, false, 0, 0); 
				gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.VIB); 
				setMatrixUniforms(); 
				gl.drawElements(gl.TRIANGLES, this.VIB.numItems, gl.UNSIGNED_SHORT, 0); 
				
			}

}
	
/* triangulo
	
    	 p1(x1,y1)
		  /\
(x2,y2)p2/__\p3(x3,y3)
 Altura.
* VPB = Vertex Position Buffer
	VCB = Vertex Color Buffer 
	VIB = Vertex Index Buffer
	VI  = Vertex Index
 */
 function Triangulo(x1,y1,x2,y2,x3,y3,z, r,g,b,o){
	this.x1=x1;
	this.y1=y1;
	this.x2=x2;
	this.y2=y2; 
	this.x3=x3;
	this.y3=y3; 

	this.vertex=[
			x1,y1,z,
			x2,y2,z,
			x3,y3,z
			];
	
	//	
	this.VPB = gl.createBuffer();
	this.VPB.itemSize=3;
	this.VPB.numItem=3;
	
	
			gl.bindBuffer(gl.ARRAY_BUFFER, this.VPB);
			
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertex), gl.STATIC_DRAW);
			
			this.VCB = gl.createBuffer(); 
			gl.bindBuffer(gl.ARRAY_BUFFER, this.VCB); 
			

			var colors = [ 
			[r, g, b, o] 
			];

			//repite cuatro veces por cada cara que es por cada vertice
			var unpackedColors = [];
			for (var i in colors) { 
				var color = colors[i]; 
				for (var j=0; j < this.VPB.numItem; j++) { 
					unpackedColors = unpackedColors.concat(color); 
					} 
			} 

			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW); 

			this.VCB.itemSize = 4;

			this.VCB.numItems = 3;
			
			this.VIB = gl.createBuffer(); 

			gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.VIB); 

			var VI = [ 
			0, 1, 2
			]; 

			gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(VI), gl.STATIC_DRAW); 

			this.VIB.itemSize = 1; 

			this.VIB.numItems = 3; 
			
			this.draw = function(){

				
				gl.bindBuffer(gl.ARRAY_BUFFER,this.VPB ); 
				gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, this.VPB.itemSize, gl.FLOAT, false, 0, 0); 
				
				gl.bindBuffer(gl.ARRAY_BUFFER, this.VCB); 
				gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, this.VCB.itemSize, gl.FLOAT, false, 0, 0); 
				
				gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.VIB); 
				
				setMatrixUniforms(); 
				gl.drawElements(gl.TRIANGLES, this.VIB.numItems, gl.UNSIGNED_SHORT, 0); 
			}
}
	
/*cuadrado
	
  p2(x2,y2,z2)	p1(x1,y1,z1)
		    |"""|
p3(x3,y3,z3)|___|p4(x4,y4,z4)

 Altura.
    
	VPB = Vertex Position Buffer
	VCB = Vertex Color Buffer 
	VIB = Vertex Index Buffer
	VI  = Vertex Index
 */
 function Cuadrado(x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4, r,g,b,o){
	this.x1=x1;
	this.y1=y1;
	this.x2=x2;
	this.y2=y2; 
	this.x3=x3;
	this.y3=y3; 

	this.vertex=[
			x1,y1,z1,
			x2,y2,z2,
			x3,y3,z3,
			x4,y4,z4
			];
	
	//	
	this.VPB = gl.createBuffer();
	this.VPB.itemSize=3;
	this.VPB.numItem=4;
	
	
			gl.bindBuffer(gl.ARRAY_BUFFER, this.VPB);
			
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertex), gl.STATIC_DRAW);
			
			this.VCB = gl.createBuffer(); 
			gl.bindBuffer(gl.ARRAY_BUFFER, this.VCB); 
			

			var colors = [ 
			[r, g, b, o] 
			];

			//repite cuatro veces por cada cara que es por cada vertice
			var unpackedColors = [];
			for (var i in colors) { 
				var color = colors[i]; 
				for (var j=0; j < this.VPB.numItem; j++) { 
					unpackedColors = unpackedColors.concat(color); 
					} 
			} 

			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW); 

			this.VCB.itemSize = 4;

			this.VCB.numItems = 4;
			
			this.VIB = gl.createBuffer(); 

			gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.VIB); 

			var VI = [ 
			0, 1, 2, 0, 2, 3, 
			]; 

			gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(VI), gl.STATIC_DRAW); 

			this.VIB.itemSize = 1; 

			this.VIB.numItems = 6; 
			
			this.draw = function(){

				
				gl.bindBuffer(gl.ARRAY_BUFFER,this.VPB ); 
				gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, this.VPB.itemSize, gl.FLOAT, false, 0, 0); 
				
				gl.bindBuffer(gl.ARRAY_BUFFER, this.VCB); 
				gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, this.VCB.itemSize, gl.FLOAT, false, 0, 0); 
				
				gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.VIB); 
				
				setMatrixUniforms(); 
				gl.drawElements(gl.TRIANGLES, this.VIB.numItems, gl.UNSIGNED_SHORT, 0); 
			}
}
	
	function PrismaTriangular(x1,y1,x2,y2,x3,y3,profundidad, r,g,b,o){
		this.x1=x1;
		this.y1=y1;
		this.x2=x2;
		this.y2=y2; 
		this.x3=x3;
		this.y3=y3; 
		
	    var triFrent= new Triangulo(x1,y1,x2,y2,x3,y3,profundidad/2, r,g,b,o)
		var triFondo= new Triangulo(x1,y1,x2,y2,x3,y3,-profundidad/2, r,g,b,o)
		var cuaDer= new Cuadrado(x1,y1,profundidad/2,
									x3,y3,profundidad/2,
									x3,y3,-profundidad/2,
									x1,y1,-profundidad/2,
									r,g,b,o)
									
		var cuaIzq = new Cuadrado(x2,y2,profundidad/2,
									 x1,y1,profundidad/2,
									 x1,y1,-profundidad/2,
									 x2,y2,-profundidad/2,
									 r,g,b,o)

		var cuaInf = new Cuadrado(x3,y3,profundidad/2,
									 x2,y2,profundidad/2,
									 x2,y2,-profundidad/2,
									 x3,y3,-profundidad/2,
									 r,g,b,o)

									 
		this.draw = function(){
		
		
            //mat4.translate(mvMatrix, [-1.5, 0.0, -17.0]);
			
			triFrent.draw();
			triFondo.draw();
			cuaDer.draw();
			cuaIzq.draw();
			cuaInf.draw();
	
		}		
}
 
 function Muslo(){
 
	var cPierna = new Cubo(0.1,0.5,1,0,0,0,1);
	//var cPierna2 = new Cubo(0.1,1,0.6,0,0,0,1);
	var pTPiernaI = new PrismaTriangular(0.12,1,-0.12,0,0.12,0,0.1,1,0,0,1);
	
	
	this.draw = function(){
		
		cPierna.draw();
		mvPushMatrix();
		mat4.rotate(mvMatrix,degToRad(-90),[1,0,0]);
		mat4.translate(mvMatrix,[-0.37,-0.5,0]);
		pTPiernaI.draw();
		mvPopMatrix();
				mvPushMatrix();
		mat4.rotate(mvMatrix,degToRad(-90),[1,0,0]);
		mat4.translate(mvMatrix,[0.37,-0.5,0]);
		mat4.rotate(mvMatrix,degToRad(180),[0,1,0]);
		pTPiernaI.draw();
			mvPopMatrix();
		
		//mat4.translate(mvMatrix, [0, 0, 0.7]);
		//cPierna2.draw();
	}
	
 }
 
 
  function Piernas(angulo){
	
	var muslo = new Muslo();
	this.angulo=angulo;
	
 
	
	this.draw = function(){
		muslo.draw();
		mvPushMatrix();
		mat4.translate(mvMatrix,[0,0,0.5]);
		mat4.rotate(mvMatrix,degToRad(-this.angulo),[1,0,0]);
		mat4.translate(mvMatrix,[0,0,0.3]);
		mat4.scale(mvMatrix, [1.9,1,0.7]);
		muslo.draw();
		mvPopMatrix();
	}
	
 }
 
 function Cuerpo(anguloBrazo, anguloPierna){
this.anguloBrazo=anguloBrazo;
this.anguloPierna=anguloPierna;

	var cuerpo = new Cubo(0.3,0.8,1.5,0.3,0.3,0.3,0.5);
	var pierna= new Piernas(10);
	
	var hombro = new PrismaTriangular(0.0,0.0,0.0,-0.8,1.5,-0.8,0.3,0.3,0.3,0.5);
	
	this.draw = function(){
	    
		mvPushMatrix();
		cuerpo.draw();
		mat4.translate(mvMatrix,[0,0,1.2]);
		pierna.angulo=this.anguloPierna;
		pierna.draw();
		mvPopMatrix();
		mvPushMatrix();
		mat4.translate(mvMatrix,[0.4,0,-0.6]);
		mat4.rotate(mvMatrix,degToRad(-90),[1,0,0]);
		mat4.rotate(mvMatrix,degToRad(-this.anguloBrazo),[0,0,1]);
		hombro.draw();
		
		mvPopMatrix();
		mvPushMatrix();
		mat4.translate(mvMatrix,[-0.4,0,-0.6]);
		mat4.rotate(mvMatrix,degToRad(-90),[1,0,0]);
		mat4.rotate(mvMatrix,degToRad(180),[0,1,0]);
		mat4.rotate(mvMatrix,degToRad(-this.anguloBrazo),[0,0,1]);
		hombro.draw();
		mvPopMatrix();
		
		
	}
	
 }

 
function degToRad(degrees) {
      return degrees * Math.PI / 180;
}
 
 var mvMatrix = mat4.create();
    var mvMatrixStack = [];
    var pMatrix = mat4.create();

    function mvPushMatrix() {
        var copy = mat4.create();
        mat4.set(mvMatrix, copy);
        mvMatrixStack.push(copy);
    }

    function mvPopMatrix() {
        if (mvMatrixStack.length == 0) {
            throw "Invalid popMatrix!";
        }
        mvMatrix = mvMatrixStack.pop();
    }
 

 function CargarMundo(data){
        var lines = data.split("\n");
        var vertexCount = 0;
        this.vertex = [];
        this.vertexColor = [];
		var x1=0;
		var y1=0;
		var z1=0;
		var x2=0;
		var y2=0;
		var z2=1;
		var x3=0;
		var y3=0;
		var z3=0;
		var i = 0;
		this.VPB = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, this.VPB);
		

		//for(i=0; i< lines.length-1; i++){
		
			var vals1 = lines[i].replace(/^\s+/, "").split(/\s+/);
			var vals2 = lines[i+1].replace(/^\s+/, "").split(/\s+/);
			x1=0;
			x2=0;
			
			//for(var j=0; j<2;j++){
				//x1 = x1+1; BEBE IR ULTIMO DEL FOR INTERNO
				y1 = parseFloat(vals1[0])/25;
				
				//x2 = x2+1;
				y2 = parseFloat(vals2[0])/25;
				
				x3 = 1;
				y2 = parseFloat(vals2[1])/25;
				
				
				this.vertex.push(x1);
				this.vertex.push(y1);
				this.vertex.push(z1);
				
				this.vertex.push(x2);
				this.vertex.push(y2);
				this.vertex.push(z2);
				
				this.vertex.push(x3);
				this.vertex.push(y3);
				this.vertex.push(z3);
				
				this.vertexColor.push(1);
				this.vertexColor.push(0);
				this.vertexColor.push(0);
				this.vertexColor.push(1);
				
				this.vertexColor.push(0);
				this.vertexColor.push(1);
				this.vertexColor.push(0);
				this.vertexColor.push(1);
				
				this.vertexColor.push(0);
				this.vertexColor.push(0);
				this.vertexColor.push(1);
				this.vertexColor.push(1);
				
				vertexCount += 3;
				
			//}
		
			z1=z1+1;
			z2=z2+1;
		//}
		alert(this.vertex);
		
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertex), gl.STATIC_DRAW);
			this.VPB.itemSize = 3;
			this.VPB.numItems = vertexCount;
			
			this.VCB = gl.createBuffer();
			gl.bindBuffer(gl.ARRAY_BUFFER, this.VCB); 
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertexColor), gl.STATIC_DRAW); 
			this.VCB.itemSize = 4;
			this.VCB.numItems = vertexCount;

			
			
			
			
			
			this.draw = function(){

			
			gl.bindBuffer(gl.ARRAY_BUFFER,this.VPB ); 
			gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, this.VPB.itemSize, gl.FLOAT, false, 0, 0); 
			
			
			gl.bindBuffer(gl.ARRAY_BUFFER, this.VCB); 
			gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, this.VCB.itemSize, gl.FLOAT, false, 0, 0); 

			
			setMatrixUniforms(); 
			//gl.drawElements(gl.TRIANGLES, this.VIB.numItems, gl.UNSIGNED_SHORT, 0); 
			gl.drawArrays(gl.TRIANGLES, 0, this.VPB.numItems);

			}
				
 }